<?php

/**
 *  Interface TestCase
 *  Test for Http Interface
 */
abstract class Test_Case_Interface extends Test_Case{

    public $http_str = false; 
    /**
     *    Creates an empty test case. Should be subclassed
     *    with test methods for a functional test case.
     *    @param string $label     Name of test case. Will use
     *                             the class name if none specified.
     *    @access public
     */
    function __construct(){
        if( !$this->http_str ){
            die("the target url can not be null!!!");
        }
        parent::__construct();
    }

    function __destruct(){
    }

    /**
     *    Get the request Url through the params
     *    @param Array               Request params
     *    @return string             Request Url
     *    @access private
     */
    protected function getUrl($params){
        array_walk($params,function(&$item,$key){
            $item = "$key=$item";
        });
        $param_str = join("&",$params);
        if(strlen($param_str) > 0){
            $param_str="?$param_str";
        }
        return "$this->http_str$param_str";
    }

    /**
     *    Get the content from the http server
     *    through the Url
     *    You should ensure that the server is 
     *    aviliable right now!!
     *    @param string         Request Url
     *    @return string        Response Content
     *    @access private
     */
    protected function execute($url){
        $handle=false;
        try{
            $handler = fopen($url,'r');
        }
        catch(Exception $e){
            throw $e;
        }
        $content="";
        if($handler){
            while(!feof($handler)){
                $content.=fread($handler,1024);
            }
        }
        else{
            throw new Exception ("connection wrong!!");
        }
        fclose($handler);
        return $content;
    }

    /**
     *    The customize Assertation for the testcase
     *    Get the resultobj from the Response Content
     *    and execute the customize logic to check
     *    if the result is correct.
     *    @param params Array           Request params
     *    @param format string          response format
     *    @param customize function     customize call function
     *    @access public
     */
    function customizeAssert($params,$format,$customize){
        try{
            $resource = $this->execute($this->getUrl($params));
            $resobj = "";
            if(strpos($resource,"var ")===0){
                $resource = preg_replace("/^[^=]*=/","",$resource);
            }
            if(strcmp($format,"json")===0){
                $resobj = json_decode($resource); 
            }
            elseif (strcmp($format,"xml") === 0){
                $resobj = simplexml_load_string($resource);
            }
            unset($resource);
            $msginfo = "";
            if(call_user_func_array($customize,array($resobj,&$msginfo))){
                unset ($resobj);
                return $this->pass($msginfo);
            }
            else{
                unset ($resobj);
                return $this->fail($msginfo);
            }
        }
        catch(Exception $e){
            $this->fail($e->getMessage());
        }
    }


}

?>
